

local mt = {}
mt.__index = mt

function mt.New(cls, name, order)
    local self = setmetatable({}, cls)
    self.world = nil
    self.name = name
    self.order = order
    self.onceHolder = {} --[]
    self.removeHolder = {} --[]
    self.updateHolder = {} --[]
    return self
end

function mt:addToWorld(world)
    self.world = world
    for idx,action in ipairs(self.onceHolder) do
        self.world:entityAdded(action.components):add(action.callback)
    end
    for idx,action in ipairs(self.removeHolder) do
        self.world:entityRemoved(action.components):add(action.callback)
    end
end

function mt:removeFromWorld()
    self.world = nil
end

function mt:update(dt)
    for idx,action in ipairs(self.updateHolder) do
        local components = action.components
        local ents = self.world:getEntities(components)
        if ents[1] then
            action.callback(dt, ents)
        end
    end
end

function mt:on(components,callback)
    if type(components) == "string" then
        components = {components}
    end
    table.insert(self.onceHolder,{components=components, callback=callback})
end

function mt:onRemove(components, callback)
    if type(components) == "string" then
        components = {components}
    end
    table.insert(self.removeHolder,{components=components, callback=callback})
end

function mt:onUpdate(components,callback)
    if type(components) == "string" then
        components = {components}
    end
    table.insert(self.updateHolder,{components=components, callback=callback})
end

return mt
